package ip.ejb.session.facade;
/***********************************************************************
 * Module:  UserFacadeBean.java
 * Author:  mojsilo
 * Purpose: Defines the Class UserFacadeBean
 ***********************************************************************/

import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import ip.dto.UserDTO;
import ip.ejb.entity.UserLocal;
import ip.ejb.entity.UserLocalHome;
import ip.ejb.entity.UserPK;
import ip.exceptions.CanNotCreateNewUserException;
import ip.exceptions.NoSuchUserException;
import ip.util.EJBHomeFactory;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;
import javax.ejb.SessionContext;
import javax.naming.NamingException;

import org.apache.log4j.Logger;
/**
 * @ejb.bean
 *   name="UserFacade"
 *   type="Stateless"
 *   jndi-name="ip/ejb/facade/UserFacade"
 *   local-jndi-name="ip/ejb/facade/UserFacadeLocal"
 * @ejb.transaction type="Supports"
 */
public class UserFacadeBean implements SessionBean
{

	private static final long serialVersionUID = 1L;
	private SessionContext ctx;
	private UserDTO userDTO = null;
	private UserLocalHome userLocalHome = null;
	private UserLocal userLocal = null;	
	private UserPK userPK = null;
	private Logger logger = Logger.getLogger(this.getClass());

	/**
	 * @throws CanNotCreateNewUserException 
	 * @ejb.interface-method view-type="local"
	 */
	public void createUser(UserDTO userDTO) throws CanNotCreateNewUserException{
		try {
			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/UserLocal");
			userLocalHome.create(
					userDTO.getFirstname(), 
					userDTO.getLastname(), 
					userDTO.getUsername(), 
					userDTO.getPassword(), 
					userDTO.getAddress(),
					userDTO.getJmbg(), 
					userDTO.getRole());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (CreateException e) {			
			e.printStackTrace();
			throw new CanNotCreateNewUserException(userDTO);
		}

	}

	/**
	 * @throws NoSuchUserException 
	 * @ejb.interface-method view-type="local"
	 */
	public void updateUser(UserDTO userDTO) throws NoSuchUserException{
		try {
			userPK = new UserPK();
			userPK.setUserId(userDTO.getUserId());
			//we are using just create(String name) method because we are doing local lookup
			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/UserLocal");
			userLocal = userLocalHome.findByPrimaryKey(userPK);

			userLocal.setAddress(userDTO.getAddress());
			userLocal.setFirstname(userDTO.getFirstname());
			userLocal.setJmbg(userDTO.getJmbg());
			userLocal.setLastname(userDTO.getLastname());
			userLocal.setPassword(userDTO.getPassword());

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchUserException(userDTO.getUsername(), userDTO.getPassword());
		}
	}

	/**
	 * @throws NoSuchUserException 
	 * @ejb.interface-method view-type="local"
	 */
	public UserDTO getUser(String username, String password) throws NoSuchUserException{
		try {
			String METHOD_NAME = "getUser(String username, String password)";
//			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/UserLocal");
			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.UserLocalHome.class);
			logger.debug("user: "+username+" - "+password);
			userLocal = userLocalHome.findUser(username, password);
			userDTO = createUserDTO(userLocal);
			logger.debug(METHOD_NAME+": "+userDTO);
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchUserException(userDTO.getUsername(), userDTO.getPassword());
		}
		return userDTO;

	}

	/**
	 * @throws NoSuchUserException 
	 * @ejb.interface-method view-type="local"
	 */
	public UserDTO getAdmin(String username, String password, String role) throws NoSuchUserException{
		try {
			logger.debug("getAdmin(String username, String password, String role): "+username+" - "+password);
//			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/UserLocal");
			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().createLocalHome(ip.ejb.entity.UserLocalHome.class);			
			userLocal = userLocalHome.findAdmin(username, password, role);
			userDTO = createUserDTO(userLocal);
			logger.debug("getAdmin(String username, String password, String role): "+userDTO);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();
			throw new NoSuchUserException(userDTO.getUsername(), userDTO.getPassword());
		}
		return userDTO;

	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public Collection getAllUsers(){
		Collection usersDTO = null;
		try {
			userLocalHome = (UserLocalHome) EJBHomeFactory.getInstance().create("ip/ejb/entity/UserLocal");
			Collection users = userLocalHome.findAll();

			if(users.size()>1){
				usersDTO= new ArrayList();
				Iterator iter = users.iterator();

				while (iter.hasNext()) {
					userLocal = (UserLocal) iter.next();
					usersDTO.add(createUserDTO(userLocal));
				}
			}

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (FinderException e) {
			e.printStackTrace();			
		}
		return usersDTO;
	}

	/**
	 * 
	 * @ejb.interface-method view-type="local"
	 */
	public UserDTO createUserDTO(UserLocal userLocal) {
		logger.debug("createUserDTO(): address: "+userLocal.getAddress());
		UserDTO userDTO = null;
		if(userLocal != null){
			userDTO = new UserDTO();
			userDTO.setUserId(userLocal.getUserId());
			userDTO.setFirstname(userLocal.getFirstname());
			userDTO.setLastname(userLocal.getLastname());
			userDTO.setPassword(userLocal.getPassword());
			userDTO.setJmbg(userLocal.getJmbg());
			userDTO.setAddress(userLocal.getAddress());
			userDTO.setUsername(userLocal.getUsername());
			userDTO.setRole(userLocal.getRole());
//			userDTO.setUserOrders(userLocal.getOrder());
		}
		return userDTO;
	}

	/**
	 * @ejb.create-method
	 */
	public void ejbCreate(){}
	public void ejbActivate(){}
	public void ejbPassivate(){}
	public void ejbRemove(){}
	public void setSessionContext(SessionContext ctx) throws EJBException, RemoteException {
		this.ctx = ctx;
	}

}